import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [
  {
    path: '/login',
    name: 'Login',
    component: () => import('../views/Login.vue'),
    meta: { isPublic: true }
  }, {
    path: '/',
    name: 'Main',
    component: () => import('../views/Main.vue'),
    redirect: '/admin_users/list',
    children: [
      {
        path: '/categories/create',
        component: () => import('../views/CategoryEdit.vue')
      }, {
        path: '/categories/list',
        component: () => import('../views/CategoryList.vue')
      }, {
        path: '/categories/edit/:id',
        component: () => import('../views/CategoryEdit.vue'),
        props: true
      },
      {
        path: '/items/create',
        component: () => import('../views/ItemEdit.vue')
      }, {
        path: '/items/list',
        component: () => import('../views/ItemList.vue')
      }, {
        path: '/items/edit/:id',
        component: () => import('../views/ItemEdit.vue'),
        props: true
      },
      {
        path: '/mings/create',
        component: () => import('../views/MingEdit.vue')
      }, {
        path: '/mings/list',
        component: () => import('../views/MingList.vue')
      }, {
        path: '/mings/edit/:id',
        component: () => import('../views/MingEdit.vue'),
        props: true
      },
      {
        path: '/summoners/create',
        component: () => import('../views/SummonerEdit.vue')
      }, {
        path: '/summoners/list',
        component: () => import('../views/SummonerList.vue')
      }, {
        path: '/summoners/edit/:id',
        component: () => import('../views/SummonerEdit.vue'),
        props: true
      },
      {
        path: '/heros/create',
        component: () => import('../views/HeroEdit.vue')
      }, {
        path: '/heros/list',
        component: () => import('../views/HeroList.vue')
      }, {
        path: '/heros/edit/:id',
        component: () => import('../views/HeroEdit.vue'),
        props: true
      },
      {
        path: '/articles/create',
        component: () => import('../views/ArticleEdit.vue')
      }, {
        path: '/articles/list',
        component: () => import('../views/ArticleList.vue')
      }, {
        path: '/articles/edit/:id',
        component: () => import('../views/ArticleEdit.vue'),
        props: true
      },
      {
        path: '/ads/create',
        component: () => import('../views/AdEdit.vue')
      }, {
        path: '/ads/list',
        component: () => import('../views/AdList.vue')
      }, {
        path: '/ads/edit/:id',
        component: () => import('../views/AdEdit.vue'),
        props: true
      },
      {
        path: '/admin_users/create',
        component: () => import('../views/AdminUserEdit.vue')
      }, {
        path: '/admin_users/list',
        component: () => import('../views/AdminUserList.vue')
      }, {
        path: '/admin_users/edit/:id',
        component: () => import('../views/AdminUserEdit.vue'),
        props: true
      }
    ]
  }
]

const router = new VueRouter({
  routes
})

// 路由守卫
router.beforeEach((to, from, next) => {
  if (!to.meta.isPublic && !localStorage.token) {
    return next('/login')
  }
  next()
})

export default router
